﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eLearning.Business;
using eLearning.Models;

namespace eLearning.Repository
{
    public class LeadAppointmentRepository : BaseManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public LeadAppointmentsModel GetAppoimentModel(int leadId)
        {
            LeadAppointmentsModel model = new LeadAppointmentsModel();

            model.DateAdd = DateTime.Now.ToShortDateString();
            model.Duration = "15 minutes";
            model.Id = leadId.ToString();
            model.ListAppointments = GetAllAppointmentByLead(leadId);
            model.ListDuration = GetDuration();

            return model;
        }

        #region Private methods
        public List<LeadAppointmentsType> GetAllAppointmentByLead(int leadId)
        {
            List<LeadAppointmentsType> list = new List<LeadAppointmentsType>();
            var items = new CalendarItemRepository().GetAllByLead(leadId);

            foreach (var item in items.OrderByDescending(c => c.StartDate))
            {
                LeadAppointmentsType model = new LeadAppointmentsType();
                model.Duration = item.DurationMins.ToString();
                model.Id = item.ItemId.ToString(); ;
                model.Reminder = item.ReminderMins.ToString() + " minutes";
                model.ShowButton = true;
                model.StartDate = item.StartDate.ToString("dd/MM/yyyy");
                model.StartTime = item.StartDate.ToString("hh:mm tt");
                model.StartTimeFormat = item.StartDate.ToString("dd/MM/yyyy hh:mm tt");
                model.Title = item.Title;
                list.Add(model);
            }

            return list;
        }
        #endregion
    }
}